\chapter{x86-64}
\label{chapter-backend-x86-64-new}

This chapter supplies the details omitted in the description of the
default calling conventions in \refChap{chap-calling-conventions}.

\section{Register usage}
\label{sec-backend-x86-64-new-register-use}

As describe in \refChap{chap-calling-conventions}, the default calling
conventions do not use registers to pass arguments.  Agreed-upon
registers are used only for the following things:

\begin{itemize}
\item the first return value,
\item the caller frame pointer upon function return,
\item the stack pointer,
\item the frame pointer,
\item the static environment argument,
\item the dynamic environment argument, and
\item the thread object.
\end{itemize}

As a result of these conventions, most registers can be used as
general-purpose registers for holding lexical variables.  The
following table shows the resulting register usage:

\begin{tabular}{|l|l|l|}
\hline
Name & Used for\\
\hline
\hline
RAX & First return value\\
RBX & Dynamic environment argument\\
RCX & Caller frame pointer upon function return\\
RDX & Register variable\\
RSP & Stack pointer \\
RBP & Frame pointer \\
RSI & Register variable\\
RDI & Register variable\\
R8  & Register variable\\
R9  & Register variable\\
R10 & Static environment argument\\
R11 & Register variable\\
R12 & Register variable\\
R13 & Register variable\\
R14 & Register variable\\
R15 & Register variable\\
FS  & Thread object\\
\hline
\end{tabular}

We use the \texttt{FS} segment register to hold an instance of the
\texttt{thread} class.  Such an instance will be used in a variety of
situations including debugging support, etc.

For information about the call-site descriptor, see
\refSec{sec-call-site-descriptor}.

\section{Calling conventions}

\subsection{Normal call}

\begin{enumerate}
\item Compute the callee function and the arguments into a temporary
  locations.
\item Load the rack of the callee function into a temporary register.
\item Load the static environment of the callee from the rack of the
  callee function object into R10.
\item Load the frame size of the callee from the rack of the callee
  function object into a temporary register, say $rf$.
\item Load the entry point from the rack into a free register, say $re$.
\item The number of arguments is known statically, say $A$.  Let $N$
  be $A$ plus $3$ ($1$ for the saved caller frame pointer, one for the
  slot to hold the return address, and $1$ for the call-site
  descriptor).
\item Store RSP in a some available register, say $rs$.
\item Subtract the contents of $rf + N \cdot 8$ from the stack
  pointer.
\item Store the caller frame pointer on the stack at location $rs -
  8$.
\item Store the call-site descriptor on the stack in $rs - 24$.
\item Store arguments into locations $RSP + 0$, $RSP + 1$, etc. but in
  reverse order, so that the last argument is on top of the stack.
\item Load the dynamic environment into RBX.
\item Store $rs$ into RBP, thereby establishing the base pointer
  for the callee.
\item Execute the CALL instruction to the entry point.
\end{enumerate}

The stack frame of the callee upon function entry is illustrated in
\refFig{fig-x86-64-alternative-stack-frame-at-entry}.

\begin{figure}
\begin{center}
\inputfig{fig-x86-64-alternative-stack-frame-at-entry.pdf_t}
\end{center}
\caption{\label{fig-x86-64-alternative-stack-frame-at-entry}
Stack frame at entry.}
\end{figure}

\subsection{Function entry}

Prelude:

\begin{enumerate}
\item Pop the return address off the stack and store it into RBP-16.
\item If required, move the static environment from R10 into some
  other lexical location, and dynamic environment from RBX into some
  other lexical location.
\item Parse the arguments and store the corresponding parameters in
  lexical locations, either in registers or in the stack frame.
\item Remove the arguments from the stack.
\end{enumerate}

After the execution of the function prelude, the contents of the stack
frame is as illustrated in
\refFig{fig-x86-64-alternative-stack-frame-after-arguments-parsed}.

\begin{figure}
\begin{center}
\inputfig{fig-x86-64-alternative-stack-frame-after-arguments-parsed.pdf_t}
\end{center}
\caption{\label{fig-x86-64-alternative-stack-frame-after-arguments-parsed}
Stack frame after arguments parsed.}
\end{figure}

% LocalWords:  callee
